package com.linkelee.manage.config;

import com.linkelee.manage.interceptor.IdentityInterceptor;
import com.linkelee.manage.interceptor.UrlInterceptor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.servlet.DispatcherServlet;
import org.springframework.web.servlet.config.annotation.*;

import java.nio.charset.Charset;
import java.util.List;

/**
 * web视图配置
 *
 * @author lsl
 * @since 2019-03-25
 */

@Configuration
public class  WebViewConfiguration implements WebMvcConfigurer {

    @Value("${utonw.allowedorigins}")
    private String[] allowedOrigins;

    @Bean
    public IdentityInterceptor identityInterceptor() {
        return new IdentityInterceptor();
    }

    @Bean
    public UrlInterceptor urlInterceptor(){
        return new UrlInterceptor();
    }

    /**
     * 配置允许访问请求后缀
     *
     * @param dispatcherServlet
     * @return
     */
    @Bean
    public ServletRegistrationBean servletRegistrationBean(DispatcherServlet dispatcherServlet) {
        ServletRegistrationBean registration = new ServletRegistrationBean(dispatcherServlet);
        registration.getUrlMappings().clear();
        registration.addUrlMappings("*.act");
        return registration;
    }

    /**
     * 配置字符信息解析器
     *
     * @return
     */
    @Bean
    public HttpMessageConverter<String> responseBodyConverter() {
        StringHttpMessageConverter converter = new StringHttpMessageConverter(
                Charset.forName("UTF-8"));
        return converter;
    }


    /**
     * 调用接口方法添加拦截器
     *
     * @param registry
     */
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(urlInterceptor())
                .addPathPatterns("/**");
        registry.addInterceptor(identityInterceptor())
                .addPathPatterns("/**")
                .excludePathPatterns("/**/*.act");
    }

    /**
     * 配置是否允许后缀地址访问
     *
     * @param configurer
     */
    @Override
    public void configurePathMatch(PathMatchConfigurer configurer) {
        configurer.setUseSuffixPatternMatch(true);
    }

    /**
     * 配置消息转换器
     *
     * @param converters
     */
    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        converters.add(responseBodyConverter());
    }

    /**
     * 配置支持拓展名
     *
     * @param configurer
     */
    @Override
    public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
        configurer.favorPathExtension(false);
    }

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins(allowedOrigins)
                .allowedHeaders("Origin","x-requested-with","Content-Type","Accept"
                        ,"X-Cookie","devicetype","token","subToken")
                .allowedMethods("GET", "POST", "PUT", "DELETE")
                .allowCredentials(false)
                .maxAge(3600);
    }

}
